package com.example.redismanual.dao.actionExamples.ZsetExamples;

import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.Set;


public class ItemRecommand {

    private ZSetOperations<String, String> client;


    public ItemRecommand(ZSetOperations<String, String> client){
        this.client = client;
    }

    private String makeRecordKey(String origin){
        return String.format("forward_to_record::%s",origin);
    }

    public void forwardTo(String origin,String destination){
        String key = this.makeRecordKey(origin);
        this.client.incrementScore(key, destination, 1);
    }

    public Set<String> pagging_record(String origin,int pageNum,int pageSize){
        String key = makeRecordKey(origin);
        int startIndex = (pageNum - 1)*pageSize;
        int endIndex = pageNum*pageSize - 1;
        return this.client.reverseRange(key,startIndex,endIndex);
    }

    public Set<ZSetOperations.TypedTuple<String>> pagging_record_with_scores(String origin, int pageNum, int pageSize){
        String key = makeRecordKey(origin);
        int startIndex = (pageNum - 1)*pageSize;
        int endIndex = pageNum*pageSize - 1;
        return this.client.reverseRangeWithScores(key,startIndex,endIndex);
    }
}
